package be.artisjaap.htmlparser.sites.electrozine;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Collection;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import be.artisjaap.htmlparser.sites.electrozine.tags.ProductInfoEzTag;
import be.artisjaap.htmlparser.sites.electrozine.tags.SitemapEzTag;
import be.artisjaap.htmlparser.tags.GoogleSearchTag;
import be.artisjaap.htmlparser.core.ASite;
import be.artisjaap.htmlparser.core.ISiteMapper;
import be.artisjaap.htmlparser.core.SiteParser;

public class ElectrozineSite extends ASite {

    @Override
    public Collection<String> getAllUrls() {
        Collection<String> result = new HashSet<String>();
        result.addAll(getAllUrlFromSite());
        result.addAll(getAllUrlsFromGoogleSearch());
        return result;

    }

    private Collection<String> getAllUrlsFromGoogleSearch() {
        Collection<String> result = new HashSet<String>();
        String url = "http://www.google.com/custom?hl=nl&safe=active&client=pub-8504106155344508&cof=FORID:9%3BAH:left%3BCX:elektrozine%3BL:http://www.google.com/intl/nl/images/logos/custom_search_logo_sm.gif%3BLH:30%3BLP:1%3BLC:%23000000%3BVLC:%23808080%3BGALT:%23191919%3B&rurl=http://www.elektrozine.be/ez/search.php%3Fcx%3Dpartner-pub-8504106155344508%253Awmgvpy-8kzi%26cof%3DFORID%253A9%26ie%3DISO-8859-1%26q%3Daeg%2Bproductdetail%26sa%3DZoeken%26siteurl%3Dwww.elektrozine.be%252Fez%252Findex.php%26google_rsg%3D__Rv8rje4vvCICRaBaJS0P5kRD5cI%3D&cx=partner-pub-8504106155344508:wmgvpy-8kzi&ad=w9&adkw=AELymgW6lm7LQ6RpUC831DoDg4Nb08-KNHujf4LsdcPLkPbOOgth02-2BvGrzNVmsVAg4XqqpFtEvsfB8yq5C7UNntk_ulqjztUuBxeKVzL1eDKlUzIiPGY&channel=4716183052&boostcse=0&oe=ISO-8859-1&ei=QdGQTcOoHsWhOr-IlaEC&q=aeg+productdetail&sa=N";
        ElectrozineSite aeg = new ElectrozineSite();
        GoogleSearchTag dtg = new GoogleSearchTag();

        for (int i = 0; i <= 99; i++) {

            String urltoparse = url + "&start=" + i * 10;
            System.out.println(urltoparse);
            String file = readUrlToDisk(urltoparse, "googlesearch-" + i + ".xml", true);

            aeg.parsePage(new File(file), dtg);
            result.addAll(dtg.getLinks());
        }
        return result;
    }

    private Collection<String> getAllUrlFromSite() {
        // String file =
        // readUrlToDisk("http://www.elektrozine.be/ez/sitemap.xml",
        // "electrozine.xml");

        SitemapEzTag sitemap = new SitemapEzTag();

        InputStream is;

        try {
            SiteParser siteParser = new SiteParser();

            is = new FileInputStream("electrozine.xml");
            siteParser.parse(is, sitemap);

            return sitemap.getProductLinks();

        }
        catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void parsePage(File file, ISiteMapper htmlmapper) {
        super.parsePage(file, htmlmapper);

        // read category from filename

        Pattern pattern = Pattern.compile("([0-9]*).xml$");
        Matcher match = pattern.matcher(file.getName());
        if (match.find()) {
            if (htmlmapper instanceof ProductInfoEzTag) {
                ((ProductInfoEzTag) htmlmapper).setPid(match.group(1));
            }
        }
    }

    public String generateFilename(String url) {
        System.out.println("get filename for: " + url);
        Pattern pattern = Pattern.compile("prodid=([0-9]*)");
        Matcher match = pattern.matcher(url);
        if (match.find())
            return "proddetails-" + match.group(1) + ".xml";
        else
            return null;

    }
}
